#include <windows.h>
#include "KUtils.h"

namespace KCS0022
{
	void MakeTable()
	{
		int i,j;
		unsigned long nCrc;
		for (i = 0; i < 256; i++)
		{
			nCrc = i;
			for (j = 0; j < 8; j++)
			{
				if (nCrc & 1)
					nCrc = (nCrc >> 1) ^ 0xEDB88320;
				else
					nCrc >>= 1;
			}
			s_Crc32Table[i] = nCrc;
		}
	}

	UINT32 GetCRC32(UINT32 nCrcInit, BYTE *pbyBuf, UINT32 nSize)
	{
		if (!pbyBuf)
			return nCrcInit;
		UINT32 nCrc = nCrcInit ^ 0xffffffff;
		while (nSize--)
			nCrc = (nCrc >> 8) ^ s_Crc32Table[(nCrc & 0xff) ^ *pbyBuf++];
		return nCrc ^ 0xffffffff;
	}
}